import axios from 'axios'
import config from '../../static/config'

import { message } from 'ant-design-vue'
// message组件配置
message.config({
  top: '100px',
  duration: 2,
  maxCount: 1 // 最大显示数量
})

/* eslint-disable */
// this.$message.config({
//     top: 100,
//     duration: 2,
//     maxCount: 1,
// });
const service = axios.create({
  baseURL: config.BASE_URL,
  // withCredentials: false,  // send cookies when cross-domain requests
  timeout: 20000, // request timeout
  // headers: { 'Content-Type': 'application/json;charset=UTF-8',
  //     'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTU4Nzk3NjIyNSwiZXhwIjoxNTg4MDYyNjI1fQ.3rGrCHv-MyLRzD2QGI1IqBjk4ihNJxY2qjERqIPEl4aNkhS--V7t-RWLwHZc1z5b6fG7T-xYTrcm1umbFQgEeA'
  // }
})
service.interceptors.request.use(
    config => {
      return config
    },
    error => {
      return Promise.reject(error)
    }
)
service.interceptors.response.use(
    response => {
        if (response.data.code==='200') {
            return response
        }else {
            message.error(response.data.message)
            throw response.data.message
        }

    },
    error => {
        if (error.response){ //超时是返回来的error.response为undefined
            switch (error.response.status) {
                case 401:
                  message.warning('登 录 权 限 失 效 ， 请 重 新 登 录')
                    setTimeout(() =>{
                        localStorage.removeItem('token');
                        sessionStorage.removeItem('aceivePath')
                        sessionStorage.removeItem('activeOpen')
                        window.location.href="./"
                    }, 1000)
                    break;
                case 500:
                  message.error("服 务 端 错 误 ")
                    break;
                case 404:
                  message.error('找 不 到 资 源 4 0 4')
                    break;
                default:
                    break
            }
        } else {
          message.error("连 接  超  时 !  请 稍 后 重 试 ")
        }
      return Promise.reject(error)
    }
)
export default service
